package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.system.domain.DataModule;
import com.ruoyi.system.domain.DataRule;
import com.ruoyi.system.mapper.DataModuleMapper;
import com.ruoyi.system.mapper.DataRuleMapper;
import com.ruoyi.system.service.DataModuleService;
import com.ruoyi.system.utils.DictUtils;
import com.ruoyi.system.vo.DataModuleAndRuleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author tang ao long
 * @Date 2020-08-21 15:15
 * @Version 1.0
 */
@Service
public class DataModuleServiceImpl implements DataModuleService {

    @Autowired
    private DataModuleMapper dataModuleMapper;

    @Autowired
    private DataRuleMapper dataRuleMapper;

    /**
     * 根据对象查询模型信息
     *
     * @param dataModule 查询对象
     * @return 返回查询list
     */
    @Override
    public List<DataModule> selectDataModuleListByInfo(DataModule dataModule) {
        return dataModuleMapper.selectDataModuleListByInfo(dataModule);
    }

    /**
     * 根据ID循环删除数据
     *
     * @param ids ID值数组
     * @return 返回删除数据的条数
     */
    @Transactional
    @Override
    public Integer deleteDataModuleByIds(String ids) {
        int count = dataModuleMapper.deleteDataModuleByIds(Convert.toStrArray(ids));
        if(count>0){
            count = dataRuleMapper.deleteDataRuleInfoByModuleId(Convert.toStrArray(ids));
            CacheUtils.removeAll(DictUtils.getCacheName());
            if(count < 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return count;
            }
        }
        return count;
    }

    /**
     * 插入一条数据模型数据
     *
     * @param dataModuleAndRuleVO 数据模型对象
     * @return 返回插入结果
     */
    @Transactional
    @Override
    public Integer insertDataModuleInfo(DataModuleAndRuleVO dataModuleAndRuleVO) {
        Integer num = dataModuleMapper.insertDataModuleInfo(dataModuleAndRuleVO);
        if (num <= 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return num;
        }

        List<DataRule> dataRules = new ArrayList<>();
        dataModuleAndRuleVO.getDataRule().forEach(key ->{

            DataRule dataRule = new DataRule();
            dataRule.setCreateId(dataModuleAndRuleVO.getCreateId());
            dataRule.setCreateName(dataModuleAndRuleVO.getCreateName());
            dataRule.setKeyCode(key);
            dataRule.setModuleId(dataModuleAndRuleVO.getId());
            dataRules.add(dataRule);

        });

        num = dataRuleMapper.insertDataRuleInfo(dataRules);
        if (num <= 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return num;
        }


        return num;
    }

    /**
     * 检查模型名称唯一性
     *
     * @param moduleName 模型名称
     * @param id         id值
     * @return 返回查询条数
     */
    @Override
    public Integer checkDataModuleNameUnique(String moduleName, String id) {
        return dataModuleMapper.checkDataModuleNameUnique(moduleName, id);
    }

    /**
     * 根据ID查询数据模型对象
     *
     * @param id 查询id
     * @return 返回查询对象
     */
    @Override
    public DataModule selectDataModuleInfoById(String id) {
        return dataModuleMapper.selectDataModuleInfoById(id);
    }

    /**
     * 根据对象修改数据模型对象
     *
     * @param dataModuleAndRuleVO 待修改的数据模型对象
     * @return 返回修改条数
     */
    @Transactional
    @Override
    public Integer updateDataModuleInfo(DataModuleAndRuleVO dataModuleAndRuleVO) {

        //修改数据模型对象信息
        Integer num = dataModuleMapper.updateDataModuleInfo(dataModuleAndRuleVO);
        if (num < 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return num;
        }

        //将表里已有的数据格式信息删除
        num = dataRuleMapper.deleteDataRuleInfoByModuleId(Convert.toStrArray(dataModuleAndRuleVO.getId().toString()));
        if (num < 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return num;
        }

        //拿到新的数据格式信息
        List<DataRule> dataRules = new ArrayList<>();
        dataModuleAndRuleVO.getDataRule().forEach(key ->{

            DataRule dataRule = new DataRule();
            dataRule.setCreateId(dataModuleAndRuleVO.getCreateId());
            dataRule.setCreateName(dataModuleAndRuleVO.getCreateName());
            dataRule.setKeyCode(key);
            dataRule.setModuleId(dataModuleAndRuleVO.getId());
            dataRules.add(dataRule);

        });

        //重新插入数据格式信息到表中
        num = dataRuleMapper.insertDataRuleInfo(dataRules);
        if (num <= 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return num;
        }

        //返回结果
        return num;
    }

}
